package com.mc.cloud.mall.cartOrder.controller;

import com.mc.cloud.mall.cartOrder.feign.UserFeignClient;
import com.mc.cloud.mall.cartOrder.model.vo.CartVO;
import com.mc.cloud.mall.cartOrder.service.CartService;
import com.mc.cloud.mall.common.common.MallRestResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CardController
 * @Author MC
 * @Date 2020/11/6 8:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("/cart")
public class CartController {
    
    @Autowired
    CartService cartService;
    
    @Autowired
    UserFeignClient userFeignClient;
    
    /**
     * 购物车列表
     * @return
     */
    @ApiOperation("购物车列表")
    @GetMapping("/list")
    public MallRestResponse list(){
    
        List<CartVO> list = cartService.cartList(userFeignClient.getUser().getId());
        return MallRestResponse.success(list);
        
    }
    
    /**
     * 添加商品到购物车
     * @param productId
     * @param count
     * @return
     */
    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public MallRestResponse add(@RequestParam Integer productId,@RequestParam Integer count){
        
        List<CartVO> list = cartService.add(productId, userFeignClient.getUser().getId(), count);
    
        return MallRestResponse.success(list);
        
    }
    
    /**
     * 更新购物车
     * @param productId
     * @param count
     * @return
     */
    @ApiOperation("更新购物车")
    @PostMapping("/update")
    public MallRestResponse update(@RequestParam Integer productId,@RequestParam Integer count){
        
        List<CartVO> cartVOList = cartService.update(productId, userFeignClient.getUser().getId(), count);
        return MallRestResponse.success(cartVOList);
        
    }
    
    /**
     * 删除购物车
     * @param productId
     * @return
     */
    @ApiOperation("删除购物车")
    @PostMapping("/delete")
    public MallRestResponse delete(@RequestParam Integer productId){
        
        List<CartVO> cartVOList = cartService.delete(productId, userFeignClient.getUser().getId());
        return MallRestResponse.success(cartVOList);
        
    }
    
    /**
     * 选中/不选中购物车的某个商品
     * @param productId
     * @param selected
     * @return
     */
    @ApiOperation("选中/不选中购物车的某个商品")
    @PostMapping("/select")
    public MallRestResponse select(@RequestParam Integer productId,@RequestParam Integer selected){
        
        List<CartVO> cartVOList = cartService.select(userFeignClient.getUser().getId(),productId,selected);
        return MallRestResponse.success(cartVOList);
        
    }
    
    /**
     * 全选/全不选购物车的某个商品
     * @param selected
     * @return
     */
    @ApiOperation("全选/全不选购物车的某个商品")
    @PostMapping("/selectAll")
    public MallRestResponse selectAll(@RequestParam Integer selected){
        
        List<CartVO> cartVOList = cartService.selectAll(userFeignClient.getUser().getId(),selected);
        return MallRestResponse.success(cartVOList);
        
    }
}
